package bean;

import java.io.File;
import java.io.IOException;

import javax.el.ExpressionFactory;
import javax.faces.context.FacesContext;

import entites.Utilisateur;

public class AppletUtilsBean {

	static final int OBJECT_NULL = -1;
	
	private String cheminAbsolu;

	public AppletUtilsBean(String underSpecFile) {
		cheminAbsolu = new String(FacesContext.getCurrentInstance().getExternalContext().getRealPath(""));
		cheminAbsolu = cheminAbsolu.substring(0, cheminAbsolu.indexOf(".metadata"));
		cheminAbsolu = cheminAbsolu.concat("cle_usb_pour_lecole"+File.separator+"WebContent"+File.separator+underSpecFile);
	}
	
	public String getCheminAbsolu() {
		return cheminAbsolu;
	}
	
	public static Object getValueExpression(FacesContext facesContext, String expr, Class<?> cla) {
		  return facesContext.getApplication().evaluateExpressionGet(facesContext, expr, cla);
		 }
		//user = (User) Utils.getValueExpression(getFacesContext(), "#{sessionScope.user}", User.class);
	//Utils.setValueExpression(fc, "#{sessionScope.user}", User.class, user);
	
	public static void setValueExpression(FacesContext facesContext, String expr, Class<?> cla, Object obj) {
	  ExpressionFactory factory = facesContext.getApplication().getExpressionFactory();
	  factory.createValueExpression(facesContext.getELContext(), expr, cla).setValue(facesContext.getELContext(), obj);
	 }

	public static void verifierConnexion() throws IOException {
		Utilisateur user = (Utilisateur) AppletUtilsBean.getValueExpression(
				FacesContext.getCurrentInstance(), "#{sessionScope.user}",
				Utilisateur.class);
		if(user==null){
			FacesContext.getCurrentInstance().getExternalContext().redirect("auth.xhtml");

		}
		
	}

}
